package com.laker.admin.module.comm.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.laker.admin.framework.aop.metrics.Metrics;
import com.laker.admin.framework.aop.trace.LakerTrace;
import com.laker.admin.module.inv.service.IInvMessageService;
import org.springframework.web.bind.annotation.RequestMapping;
import com.laker.admin.module.comm.entity.CommProduct;
import com.laker.admin.module.comm.service.ICommProductService;
import org.springframework.web.bind.annotation.RestController;
import com.laker.admin.framework.model.Response;
import com.laker.admin.framework.model.PageResponse;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author laker
 * @since 2024-03-20
 */
@RestController
@RequestMapping("/comm/product")
@Metrics
@LakerTrace
public class CommProductController {
    @Autowired
    ICommProductService commProductService;

    @Autowired
    IInvMessageService invMessageService;

    @GetMapping
    @ApiOperation(value = "分页查询")
    public PageResponse pageAll(@RequestParam(required = false, defaultValue = "1") long page,
                                @RequestParam(required = false, defaultValue = "10") long limit,
                                String keyWord) {
        Page roadPage = new Page<>(page, limit);
        LambdaQueryWrapper<CommProduct> queryWrapper = new QueryWrapper().lambda();

        queryWrapper.eq(CommProduct::getDelFlag, 0).and(StrUtil.isNotBlank(keyWord), i -> i.
                like(StrUtil.isNotBlank(keyWord), CommProduct::getCommName, keyWord)
                .or().like(StrUtil.isNotBlank(keyWord), CommProduct::getCommSpecs, keyWord));

        Page pageList = commProductService.page(roadPage, queryWrapper);
        return PageResponse.ok(pageList.getRecords(), pageList.getTotal());
    }

    @PostMapping
    @ApiOperation(value = "新增或者更新")
    public Response saveOrUpdate(@RequestBody CommProduct param) {
        if (param.getCommTypeId() == null) {
            return Response.error("500", "请选择类型");
        }

        if ("".equals(param.getSpecsIds()) || param.getSpecsIds() == null) {
            return Response.error("500", "请选择规格");
        }

        return Response.ok(commProductService.saveOrUpdateProduct(param));
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询")
    public Response get(@PathVariable Long id) {
        return Response.ok(commProductService.getById(id));
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除")
    public Response delete(@PathVariable Long id) {
        // 判断是否有库存 如果有库存则不允许删除
        int invCount = invMessageService.selectCountByCommId(id);
        if (invCount > 0) {
            return Response.error("500", "该商品下存在库存，不允许删除！");
        }

        // 删除库存
        invMessageService.deleteByCommid(id);
        return Response.ok(commProductService.removeById(id));
    }

    @DeleteMapping("/batch/{ids}")
    @ApiOperation(value = "根据批量删除ids删除")
    public Response batchRemove(@PathVariable Long[] ids) {
        // 判断是否有库存 如果有库存则不允许删除
        for (Long id : ids) {
            int invCount = invMessageService.selectCountByCommId(id);
            if (invCount > 0) {
                return Response.error("500", "ID为" + id + "的商品下存在库存，不允许删除！");
            }
        }

        // 删除库存
        for (Long id : ids) {
            invMessageService.deleteByCommid(id);
        }

        return Response.ok(commProductService.removeByIds(CollUtil.toList(ids)));
    }
}
